package com.simpns.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import com.simpns.io.IORet;
import com.simpns.io.Line7IoDevice;
import com.simpns.io.entity.DevReturn;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

import java.net.URL;
import java.util.ResourceBundle;

public class MainController implements Initializable {
    public TextArea info;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        info.setFont(Font.font("思源黑体 CN", FontWeight.NORMAL, 15));
    }

    private static String getString(String desc, IORet ioRet) {
        return DateUtil.now() + desc + " ==> " + ioRet.getResultDesc() + "\n";
    }

    public void setComAction() {
        IORet ioRet = Line7IoDevice.setCommPra();
        info.appendText(getString("设置串口", ioRet));
    }

    public void initAction() {
        IORet ioRet = Line7IoDevice.init();
        info.appendText(getString("初始化", ioRet));
    }

    public TextField lightNum;

    public void getLightStatusAction() {
        String lightNumText = lightNum.getText();
        IORet ioRet = Line7IoDevice.getLightStatus(Integer.parseInt(lightNumText));
        DevReturn devReturns = ioRet.getDevReturns();
        info.appendText("结果: " + ioRet.getMessage() + "\n");
        //输出devReturns的内容
        //* 通过p_psStatus的8维结构，可以返回2种指示灯的状态，当灯灭的时候返回0，当灯亮的时候返回1。
        for (int i = 0; i < 13; i++) {
            info.appendText("led[" + i + "]: " + devReturns.acDevReturn[i] + "\n");
        }
    }

    public TextField lightNum1;
    public TextField lightStatus;

    public void setLightStatusAction() {
        String lightNum1Text = lightNum1.getText();
        String lightStatusText = lightStatus.getText();
        IORet ioRet = null;
        if (lightNum1Text.contains(",")) {
            // Split the string by comma
            String[] parts = lightNum1Text.split(",");
            int[] lightNums = new int[parts.length];
            // Convert each part to an integer
            for (int i = 0; i < parts.length; i++) {
                lightNums[i] = Integer.parseInt(parts[i].trim());
            }
            String[] statusTemp = lightStatusText.split(",");
            int[] lightStatuses = new int[statusTemp.length];
            // Assume lightStatusText is a single value, repeat it for all lights
            for (int i = 0; i < lightStatuses.length; i++) {
                lightStatuses[i] = Integer.parseInt(statusTemp[i].trim());
            }
            ioRet = Line7IoDevice.setLightStatusEx(lightNums, lightStatuses);
        }else {
            ioRet = Line7IoDevice.setLightStatus(Integer.parseInt(lightNum1Text), Integer.parseInt(lightStatusText));
        }
        info.appendText(getString("设置灯状态", ioRet));
    }

    public TextField doorNum;

    public void getDoorStatusAction() {
        String doorNumText = doorNum.getText();
        IORet ioRet = Line7IoDevice.getDoorStatus(Integer.parseInt(doorNumText));
        DevReturn devReturns = ioRet.getDevReturns();
        info.appendText("结果: " + ioRet.getMessage() + "\n");
        //输出devReturns的内容
        //* 通过p_psStatus的8维结构，可以返回2种指示灯的状态，当灯灭的时候返回0，当灯亮的时候返回1。
        for (int i = 0; i < 13; i++) {
            info.appendText("door[" + i + "]: " + devReturns.acDevReturn[i] + "\n");
        }
    }

    public void getIoStatusAction() {
        IORet ioRet = Line7IoDevice.getIoBoardStatus();
        info.appendText(getString("获取io状态", ioRet));
    }

    public void getVersionAction() {
        String version = Line7IoDevice.getVersion();
        info.appendText(DateUtil.now() + "获取版本号 ==> " + version);
    }
}
